// pages/authorize/index.js
var app = getApp();
var url = app.globalData.ApiUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mobile: '',
    showMobile: true
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onLoad: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindGetUserInfo: function (e) {
    wx.getUserInfo({
      success: (res) => {
        wx.setStorageSync("userInfo", res.userInfo);
        var openid = wx.getStorageSync('openid');
        var login_secret = wx.getStorageSync('login_secret');
        var nickname = wx.getStorageSync('userInfo').nickName;
        var headimgurl = wx.getStorageSync('userInfo').avatarUrl;
        var unionid = wx.getStorageSync("unionid");
        var data = {
          openid: openid,
          login_secret: login_secret,
          applet_type: 'applet_wx',
          nickname: nickname,
          headimgurl: headimgurl,
        };
        if (unionid != '') {
          data.unionid = unionid;
        }
        wx.request({
          url: url + '/mobile/index.php?act=connect&op=get_applet_info',
          header: {
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          dataType: 'json',
          method: 'post',
          data: data,
          success: (res) => {
            if (res.data.code == 200) {
              wx.showLoading({
                title: '加载中',
                mask: true
              });
              setTimeout(function () {
                wx.hideLoading();
              }, 2000)
              wx.setStorageSync('key', res.data.datas.key);
              wx.setStorageSync('userid', res.data.datas.userid);
              wx.setStorageSync('username', res.data.datas.username);
              var member_mobile = 'member_mobile' in res.data.datas ? res.data.datas.member_mobile : '';
              wx.setStorageSync("mobile", member_mobile);
              wx.switchTab({
                url: '/pages/index/index',
              })
            } else {
              wx.clearStorageSync();
              app.login();
            }
          },
          fail: () => {
            wx.showToast({
              title: '登录失败',
              icon: 'none',
              duration: 3000
            })
          }
        })
      },
      fail: function () {
        wx.switchTab({
          url: '/pages/index/index'
        })
      }
    })
  },
  
})